package com.lry;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
public class Master {

    private List<Slave> slaves;

    public Master(){
        slaves = new ArrayList<>();
        log.info("启动master...");
    }

    public void register(Slave... inSlaves){
        for (Slave slave : inSlaves) {
            if(null==slave){
                continue;
            }
            log.info("注册slave{}", slave);
            slaves.add(slave);
        }
    }


    private void broadcast(Integer value){
        for (Slave slave : slaves) {
            CompletableFuture.runAsync(()->{
                log.info("master广播{}到slave{}",value,slave);
                slave.receiveFromMaster(value);
            });
        }
    }


    public void setValue(Integer value){
        broadcast(value);
    }

}
